package com.yxhl.zoume.utils.permission;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.v4.app.Fragment;

import com.yxhl.zoume.R;

/**
 * Created by sugarya on 16/04/05.
 * 权限开启辅助类,直接面向app的权限开启的业务逻辑
 */
public class PermissionHelper {
    public static final int REQUEST_CODE_CALL_TRIP_DETAIL = 17;
    /**
     * 个人中心页面，打电话用到的requestCode
     */
    public static final int REQUEST_CODE_CALL_USER_CENTER = 20;
    /**
     * 快车地图,打电话给接驾司机
     */
    public static final int REQUEST_CODE_CALL_SPECIAL_CAR_DRIVER = 21;
    /**
     * 快车地图,打电话给客服
     */
    public static final int REQUEST_CODE_CALL_CUSTOMER_SERVICE = 22;
    public static final int REQUEST_CODE_STATION_MAP_DEPART_LOCATION = 18;
    public static final int REQUEST_CODE_STATION_MAP_ARRIVAL_LOCATION = 19;
    public static final int REQUEST_CODE_SCHEDULE_MAP_LOCATION = 24;
    public static final int REQUEST_CODE_WRITE_EXTERNAL_STORAGE = 25;
    public static final int REQUEST_CODE_ZOU_ME_POSITION_LOCATION = 26;
    public static final int REQUEST_CODE_READ_SMS = 27;
    public static final int REQUEST_CODE_OFFICIAL_CAR_SINGLE_WAY_GET_IN_LOCATION = 28;
    public static final int REQUEST_CODE_OFFICIAL_CAR_GET_OFF_LOCATION = 29;
    public static final int REQUEST_CODE_READ_CONTACT = 30;
    public static final int REQUEST_CODE_OFFICIAL_CAR_RENT_HOURLY_GET_IN_LOCATION = 31;
    public static final int REQUEST_CODE_EXPRESS_CAR_SINGLE_WAY_GET_IN_LOCATION = 32;
    public static final int REQUEST_CODE_EXPRESS_CAR_SINGLE_WAY_GET_OFF_LOCATION = 33;
    public static final int REQUEST_CODE_EXPRESS_CAR_RENT_HOURLY_GET_IN_LOCATION = 34;
    private static final String TAG = "PermissionHelper";
    /**
     * 权限适配最低兼容版本号
     */
    private static final int PERMISSION_LIMIT = 23;

    /**
     * 检测是否有读取联系人权限
     *
     * @param activity
     * @return
     */
    public static boolean checkReadContactPermission(Activity activity) {

        if (Build.VERSION.SDK_INT >= PERMISSION_LIMIT) {
            return PermissionUtils.checkGroupPermissions(activity, new String[]{Manifest.permission.READ_CONTACTS});
        }

        return true;
    }

    /**
     * 请求读取联系人权限
     *
     * @param fragment
     * @param requestCode
     * @return
     */
    public static boolean takeReadContactPermission(Fragment fragment, int requestCode) {
        if (Build.VERSION.SDK_INT >= PERMISSION_LIMIT) {
            PermissionUtils.takeOnePermission(
                    fragment,
                    Manifest.permission.READ_CONTACTS,
                    fragment.getString(R.string.permission_read_contact_alert_message),
                    requestCode
            );
        }
        return true;
    }


    /**
     * 检查有无打电话的权限
     * api level < 23 的话，不需要申请这个权限，因此直接返回true
     *
     * @param activity
     * @return
     */
    public static boolean checkPhonePermission(Activity activity) {
        if (Build.VERSION.SDK_INT >= PERMISSION_LIMIT) {
            return PermissionUtils.checkGroupPermissions(activity, new String[]{Manifest.permission.CALL_PHONE});
        } else {
            return true;
        }
    }

    /**
     * 申请打电话的权限
     * 申请权限应该在检查权限之后进行，因此内部无需再做判断
     *
     * @param activity
     * @param requestCode
     */
    public static void takeCallPhonePermission(Activity activity, int requestCode) {
        PermissionUtils.takeOnePermission(
                activity,
                Manifest.permission.CALL_PHONE,
                activity.getString(R.string.permission_call_phone_alert_message),
                requestCode
        );
    }

    public static boolean checkReadSmsPermission(Context context) {
        if (Build.VERSION.SDK_INT >= PERMISSION_LIMIT) {
            return PermissionUtils.checkGroupPermissions(context, new String[]{Manifest.permission.READ_SMS});
        } else {
            return true;
        }
    }

    /**
     * 开启读取短信权限
     * Ethan
     *
     * @param activity
     * @param requestCode
     */
    public static boolean takeReadSmsPermission(Activity activity, int requestCode) {
        if (Build.VERSION.SDK_INT >= PERMISSION_LIMIT) {
            PermissionUtils.takeOnePermission(
                    activity,
                    Manifest.permission.READ_SMS,
                    activity.getString(R.string.permission_pdus_alert_message),
                    requestCode
            );
        }
        return true;
    }

    /**
     * 开启读取短信权限
     * Ethan
     *
     * @param fragment
     * @param requestCode
     */
    public static boolean takeReadSmsPermission(Fragment fragment, int requestCode) {
        if (Build.VERSION.SDK_INT >= PERMISSION_LIMIT) {
            PermissionUtils.takeOnePermission(
                    fragment,
                    Manifest.permission.READ_SMS,
                    fragment.getString(R.string.permission_pdus_alert_message),
                    requestCode
            );
        }
        return true;
    }

    /**
     * 检查App是否有定位权限
     * Ethan
     *
     * @param activity
     * @return true ,已授权,false,未授权
     */
    public static boolean checkLocationPermission(Activity activity) {
        if (Build.VERSION.SDK_INT >= PERMISSION_LIMIT) {
            return PermissionUtils.checkGroupPermissions(activity, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION});
        } else {
            return true;
        }
    }

    /**
     * 申请定位权限
     * Ethan
     *
     * @param activity
     * @param requestCode
     */
    public static void takeLocationPermission(Activity activity, int requestCode) {
        if (Build.VERSION.SDK_INT >= PERMISSION_LIMIT) {
            PermissionUtils.takeGroupPermissions(
                    activity,
                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                    new String[]{activity.getString(R.string.permission_location_alert_message)},
                    requestCode
            );
        }
    }

    /**
     * 申请定位权限
     * Ethan
     *
     * @param fragment
     * @param requestCode
     */
    public static void takeLocationPermission(Fragment fragment, int requestCode) {
        if (Build.VERSION.SDK_INT >= PERMISSION_LIMIT) {
            PermissionUtils.takeGroupPermissions(
                    fragment,
                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                    new String[]{fragment.getString(R.string.permission_location_alert_message)},
                    requestCode
            );
        }
    }

    /**
     * 检查App是否有写SD卡权限
     *
     * @param activity
     * @return
     */
    public static boolean checkWriteExternalStorage(Activity activity) {
        if (Build.VERSION.SDK_INT >= PERMISSION_LIMIT) {
            return PermissionUtils.checkGroupPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE});
        } else {
            return true;
        }
    }

    public static void takeWriteExternalStoragePermission(Activity activity) {
        if (Build.VERSION.SDK_INT >= PERMISSION_LIMIT) {
            PermissionUtils.takeGroupPermissions(
                    activity,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    new String[]{activity.getString(R.string.permission_write_external_storage_alert_message)},
                    REQUEST_CODE_WRITE_EXTERNAL_STORAGE
            );
        }
    }
}
